//
//  ProductListUIV.m
//  mbstore
//
//  Created by Juvid on 14-4-16.
//  Copyright (c) 2014年 huangyi. All rights reserved.
//

#import "ProductListUIV.h"
#import "MBProgressHUD.h"
#import "HomeUI.h"

@implementation ProductListUIV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate=self;
        self.dataSource=self;
        self.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsHorizontalScrollIndicator=NO;
        self.showsVerticalScrollIndicator=NO;
        self.backgroundColor=[UIColor clearColor];
        self.bounces=NO;
       [self.layer setBorderWidth:1];
        [self.layer setBorderColor:[UIColor lightGrayColor].CGColor];
       
        [self reloadData];

    }
    return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    //    if (dicGetDetail.count>0) {
    return 1;
    //    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier=@"pdt";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.clipsToBounds=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    else{
        while ([cell.contentView.subviews lastObject] != nil)
        {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
        
    }
    
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            UILabel *labName=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 67)];
            labName.text=@"主要卖点";
            labName.textAlignment=NSTextAlignmentCenter;
            labName.font=[UIFont systemFontOfSize:14];
            [cell.contentView addSubview:labName];
            
            
            UILabel *imgLine=[[UILabel alloc]initWithFrame:CGRectMake(0, 67, self.frame.size.width, 3)];
            imgLine.backgroundColor=[UIColor clearColor];
            imgLine.text=@" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
            imgLine.textColor=[UIColor darkGrayColor];
            imgLine.font=[UIFont systemFontOfSize:9];
            [cell.contentView addSubview:imgLine];
            for (int i=0; i<3; i++) {
                UILabel *labCheck=[[UILabel alloc]initWithFrame:CGRectMake(10+72*i, 80, 62, 25)];
                labCheck.text=@"标签";
                labCheck.backgroundColor=[UIColor lightGrayColor];
                labCheck.textColor=[UIColor blackColor];
                labCheck.textAlignment=NSTextAlignmentCenter;
                [cell.contentView addSubview:labCheck];
            }
        }
        
    }
    else if(indexPath.section==1){
        for (int i=0; i<3; i++) {
            UILabel *labSize=[[UILabel alloc]initWithFrame:CGRectMake(10, 10+i*29, 86, 30)];
            UILabel *labStock=[[UILabel alloc]initWithFrame:CGRectMake(95, 10+i*29, 56, 30)];
            UILabel *labAddCart=[[UILabel alloc]initWithFrame:CGRectMake(150, 10+i*29, 100, 30)];
            [labSize.layer setBorderWidth:1];
            [labSize.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            [labAddCart.layer setBorderWidth:1];
            [labAddCart.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            [labStock.layer setBorderWidth:1];
            [labStock.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            
            labStock.font=[UIFont systemFontOfSize:15];
            labSize.font=[UIFont systemFontOfSize:15];
            labAddCart.font=[UIFont systemFontOfSize:15];
            
            labStock.textAlignment=NSTextAlignmentCenter;
            labSize.textAlignment=NSTextAlignmentCenter;
            labAddCart.textAlignment=NSTextAlignmentCenter;
            
            [cell.contentView addSubview:labStock];
            [cell.contentView addSubview:labAddCart];
            [cell.contentView addSubview:labSize];
            if (i==0) {
                labSize.backgroundColor=[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:229.0/255.0 alpha:1];
                labStock.backgroundColor=[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:229.0/255.0 alpha:1];
                labAddCart.backgroundColor=[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:229.0/255.0 alpha:1];
                labSize.text=@"尺寸" ;
                labAddCart.text=@"加入购物车";
                labStock.text=@"库存";
                
            }
            else {
                UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 10+i*29, 100, 30)];
                [btn setTitle:@"添加" forState:UIControlStateNormal];
                [cell.contentView addSubview:btn];
                btn.titleLabel.font=[UIFont systemFontOfSize:15];
                [btn addTarget:self action:@selector(AddChar:) forControlEvents:UIControlEventTouchUpInside];
                if (_dicDetail) {
                    labSize.text=_dicDetail[@"size_name"];
                    labStock.text=@"11";
                    
                }
            }

        }
//        for (int i=0; i<4; i++) {
//            UILabel *labLine=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 250, 1)];
//            labLine.backgroundColor=[UIColor lightGrayColor];
//            [cell.contentView addSubview:labLine];
//
//        }
        
    }
    else if(indexPath.section==2){
        
        self.colorScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 75)];
        //            scrColor.backgroundColor=[UIColor blueColor];
        [cell.contentView addSubview:self.colorScrollView];
         UIButton *cb=nil;
        for (int i=0; i<3; i++) {
            UIButton *button=[self colorButton:nil];
            button.frame=CGRectOffset(button.bounds, 5+(CGRectGetWidth(button.bounds))*i, 8);
            [self.colorScrollView addSubview:button];
            self.colorScrollView.contentSize=CGSizeMake(CGRectGetMaxX(button.frame), CGRectGetHeight(button.frame));
            cb=button;
            
            
            [self onColorClick:cb];

        }
        
    }
    //    cell.textLabel.text=@"fslkahgl";
    return cell;
}

-(UIButton*)colorButton:(NSDictionary*)sku{
//    NSString *color=sku[@"color_name"];
//    NSString *image=sku[@"main_photo"];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.payloadObject=sku;
    [button addTarget:self action:@selector(onColorClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame=CGRectMake(0, 0, 50, 60);
    [button setTitle:@"白色" forState:UIControlStateNormal];
    [button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font=[UIFont systemFontOfSize:12];
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 40, 40)];
    imageView.tag=1001;
    [button addSubview:imageView];
//    image = [image stringByDeletingPathExtension];
//    image = [NSString stringWithFormat:@"%@--w_40_h_40.jpg",image];
//    NSString *imageURL=[AppUI getImageURL:image];
//    [imageView setImageURL:imageURL];
    return button;
}
-(void)onColorClick:(id)sender{
    for (id view in self.colorScrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button=view;
            if (button==sender) {
                UIView *view=[button viewWithTag:1001];
                view.layer.borderColor=[[UIColor redColor] CGColor];
                view.layer.borderWidth=1;
//                self.sku[@"color_name"]=button.payloadObject[@"color_name"];
//                NSString *imageName =button.payloadObject[@"main_photo"];
//                imageName = [imageName stringByDeletingPathExtension];
//                imageName = [NSString stringWithFormat:@"%@--w_320_h_320.jpg",imageName];
//                NSString *imageURL=[AppUI getImageURL:imageName];
//                self.itemImage.image = nil;
//                [self.itemImage setImageURL:imageURL];
            }else{
                UIView *view=[button viewWithTag:1001];
                view.layer.borderColor=[[UIColor grayColor] CGColor];
                view.layer.borderWidth=1;
            }
        }
    }
//    [self checkStock];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat Heigh=0.0;
    if (indexPath.section==0) {
        Heigh=120;
    }
    if (indexPath.section==1) {
        Heigh=120;
    }
    if (indexPath.section==2) {
        Heigh= 70;
    }
    return Heigh;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }
    return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *vieResult=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, CGRectGetHeight([self rectForHeaderInSection:section]))];
    
    [vieResult.layer setBorderWidth:1];
    [vieResult.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    
    UILabel *result=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, vieResult.frame.size.height)];
    result.backgroundColor=[UIColor colorWithRed:229.0/255.0 green:229.0/255.0 blue:229.0/255.0 alpha:1];
    result.font=[UIFont systemFontOfSize:14];
    [vieResult addSubview:result];
    if (section==1) {
        result.text=@"  当前颜色库存";
    }
    else if(section==2) {
        result.text=@"  其他颜色";
    }
   
    return vieResult;
}
-(void)AddChar:(UIButton *)sender{
#warning 加入购物车
    UIButton *buy=sender;
    NSString *itemId=buy.payloadObject;
    if (itemId) {
        ItemUI *item=self.itemDelegate;
        item.productId=itemId;
    }
//    NSString *skuId=[self getSkuId];
//    MainApp *app=[MainApp mainApp];
//    [app.shoppingList addObject:skuId];
//    [app requestProduct:skuId];
    [[NSNotificationCenter defaultCenter] postNotificationName:ShoppingListNotification object:self];
//
//    MBProgressHUD *hud=[[MBProgressHUD alloc] initWithView:self.superview];
//    hud.mode=MBProgressHUDModeText;
//    hud.labelText=@"成功添加到购物车";
//    [self.superview addSubview:hud];
//    [hud show:YES];
//    [hud hide:YES afterDelay:1.0];
//    NSLog(@"添加成功");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
